﻿Imports Pop.Api.Util

Namespace Pop.Api.Request

    ''' <summary>
    ''' POP请求基类
    ''' </summary>
    Public MustInherit Class PopRequest(Of T As PopResponse)
        Implements IPopRequest(Of T)

        Private Const m_CHARSET As String = "charset"
        Private Const m_FORMAT As String = "format"
        Private Const m_PUREDATA As String = "pureData"

        ''' <summary>
        ''' 用于指定request和response的编码(gbk,utf-8)
        ''' </summary>
        Property Charset As String = "utf-8"

        ''' <summary>
        ''' 输出格式,默认xml
        ''' </summary>
        Property Format As String = "xml" Implements IPopRequest(Of T).Format

        ''' <summary>
        ''' json格式设置,默认值为0
        ''' </summary>
        Property PureData As Nullable(Of Long)

#Region "MustOverride"
        Protected MustOverride Function GetApiName() As String Implements IPopRequest(Of T).GetApiName
        Protected MustOverride Function GetParameters() As IEnumerable(Of KeyValuePair(Of String, String))
        Protected MustOverride Sub Validate() Implements IPopRequest(Of T).Validate
#End Region

#Region "IPopRequest"

        Public Function GetAllParameters() As IEnumerable(Of KeyValuePair(Of String, String)) Implements IPopRequest(Of T).GetParameters
            Dim parameters As New PopDictionary(GetParameters)
            parameters.Add(m_CHARSET, Charset)
            parameters.Add(m_FORMAT, Format)
            parameters.Add(m_PUREDATA, PureData)
            Return parameters
        End Function

        Public Function ServerUrl() As String Implements IPopRequest(Of T).ServerUrl
            Dim apiName As String = GetApiName.Replace(".", "/")
            If Charset Is Nothing OrElse String.IsNullOrWhiteSpace(Charset) Then
                Return String.Format("http://api.paipai.com/{0}.xhtml", apiName)
            Else
                Return String.Format("http://api.paipai.com/{0}.xhtml?charset={1}", apiName, Charset)
            End If
        End Function

#End Region

    End Class

End Namespace